import { isLocal } from '@/env'
import store from '@/store'
import devConfig from '../../../dev-config'

export function mapPageUrl (rawUrl) {
  if (rawUrl && rawUrl[0] !== '/') {
    rawUrl = '/' + rawUrl
  }

  // TODO: d90&l22 subpages change to packages
  return `/subpages/${devConfig.name}${rawUrl}`
}

export function asyncRequest (address) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: address,
      success: (res) => {
        resolve(res)
      },
      fail: (res) => {
        console.log('请求失败', res)
        reject(res)
      }
    })
  })
}

export function mapAssetSrc (src) {
  if (isLocal()) {
    return process.env.LOCAL_SERVER + '/static/' + src
  }
  return !store.state.assetMap || !store.state.assetMap[src] ? '' : store.state.assetMap[src].replace(/\\/g, '/')
}

export function goActivity (id) {
  if (!isLocal()) {
    getApp().goActivity(id)
  }
}

export function getScaleValue () {
  return uni.upx2px(750) / 750
}
